<template>
    <div class="form-block-inner">
        <Row class="add-header" type="flex" justify="space-between">
            <Col>
                <div class="button-group">
                    <Button type="primary" @click="saveBtnAction">保存</Button>
                </div>
            </Col>
        </Row>
        <worker-info :form-item="formItem"></worker-info>
    </div>
</template>

<script>
import workerInfo from '@/views/pages/worker/add/components/worker-info/index'
import { getPeopleById, savePeople } from '@/api/worker'

export default {
  name: 'index',
  components: {
    workerInfo
  },
  data () {
    const checkidcard = (rule, value, callback) => {
      if (!this.$common.identityCodeValid(value)) {
        callback(new Error('身份证号不合法！'))
      } else {
        callback()
      }
    }
    const checkname = (rule, value, callback) => {
      if (value === '' || value === undefined) {
        callback(new Error('姓名不能为空！'))
      } else {
        callback()
      }
    }
    const checktui_xiu_date = (rule, value, callback) => {
      if (value === '' || value === undefined) {
        callback(new Error('退休时间不能为空！'))
      } else {
        callback()
      }
    }
    const checkwork_name = (rule, value, callback) => {
      if (value === '' || value === undefined) {
        callback(new Error('工作单位不能为空！'))
      } else {
        callback()
      }
    }
    return {
      name: '0',
      row: {},
      formItem: {},
      date: '',
      ruleCustom: {
        name: [
          { validator: checkname, trigger: 'blur' }
        ],
        idcard: [
          { validator: checkidcard, trigger: 'blur' }
        ],
        work_name: [
          { validator: checkwork_name, trigger: 'blur' }
        ],
        tui_xiu_date: [
          { validator: checktui_xiu_date, trigger: 'blur' }
        ]

      }
    }
  },
  methods: {
    newInfo () {
      this.formItem = {
        name: '测试名字',
        card_no: '211381198712180451',
        work_name: '饿了么',
        phone: '13811111111'
      }
    },
    saveBtnAction () {
      if (this.formItem.name === '' || this.formItem.name === undefined || this.formItem.name === null) {
        this.$Message.error('请输入姓名！')
        return false
      }
      if (!this.$common.identityCodeValid(this.formItem.card_no)) {
        this.$Message.error('身份证不合法！')
        return false
      }
      if (this.formItem.phone === '' || this.formItem.phone === undefined || this.formItem.phone === null) {
        this.$Message.error('请输入手机号！')
        return false
      }
      if (this.formItem.work_name === '' || this.formItem.work_name === undefined || this.formItem.work_name === null) {
        this.$Message.error('请输入单位名称！')
        return false
      }
      if (this.formItem.longitude === '' || this.formItem.longitude === undefined || this.formItem.longitude === null) {
        this.$Message.error('请输入经度！')
        return false
      }
      if (this.formItem.latitude === '' || this.formItem.latitude === undefined || this.formItem.latitude === null) {
        this.$Message.error('请输入纬度！')
        return false
      }
      if (this.formItem.type === '' || this.formItem.type === undefined || this.formItem.type === null) {
        this.$Message.error('请选择类型！')
        return false
      }
      if (this.formItem.health_type === '' || this.formItem.health_type === undefined || this.formItem.health_type === null) {
        this.$Message.error('请选择健康状态！')
        return false
      }
      if (this.formItem.person_type === '' || this.formItem.person_type === undefined || this.formItem.person_type === null) {
        this.$Message.error('请选择人员状态！')
        return false
      }
      if (this.$route.query.id !== undefined && this.$route.query.id !== '' && this.$route.query.id !== null) {
        this.formItem.id = this.$route.query.id
      }
      savePeople(this.formItem).then(ret => {
        if (ret.data.errcode === 0) {
          this.$Message.info('保存成功')
          this.$router.push({ path: '/worker/list' })
        } else {
          this.$Message.error('获取信息失败，请检查网络！')
        }
      })
    }
  },
  mounted () {
    if (this.$route.query.what === 'add') {
      // this.newInfo()
    } else {
      getPeopleById({ id: this.$route.query.id }).then(ret => {
        if (ret.data.errcode === 0) {
          this.formItem = ret.data.data
        } else {
          this.$Message.error('获取信息失败，请检查网络！')
        }
      })
    }
  }
}
</script>

<style scoped>
    .add-header {
        margin-bottom: 10px;
    }
</style>
